import axios from 'axios'
export default function ajax(url, data={}, type='GET'){
   return new Promise(function (resole, reject){
        // 执行一部ajax请求
        let promise
        if(type === 'GET'){
            // 准备url query 参数数据
            let dataStr = ''// 数据拼接字符串
            Object.keys(data).forEach(key => {
                dataStr += key + '=' + data[key] + '&'
            })
            if(dataStr !== ''){
                dataStr = dataStr.substriing(0, dataStr.lastIndexOf('&'))
                url = url + '?' + dataStr
            }
            // 发送get请求
            promise = axios.get(url)
        }else{
            // 法搜post请求
            promise = axios.post(url, data)
        }
        promise.then(function(response){
            // 成功调用resole()
            resole(response.data)
        }).then(function (error){
            // 失败调用reject()
            reject(error)
        })
        
    })
}